//
//  XiehouView.m
//  Liuhe
//
//  Created by zr on 2018/8/16.
//  Copyright © 2018年 zr. All rights reserved.
//

#import "XiehouView.h"


@interface XiehouView ()

@property (nonatomic, weak)UIImageView * backImageView;
@property (nonatomic, weak)UIView * bottomView;
@property (nonatomic, weak)UIView * lineView;
@property (nonatomic, weak)UILabel * nameLabel;
@property (nonatomic, weak)UILabel * ageLabel;
@property (nonatomic, weak)UILabel * startLabel;
@property (nonatomic, weak)UILabel * adressLabel;
@property (nonatomic, weak)UILabel * timeLabel;
@property (nonatomic, weak)UILabel * contentLabel;
@property (nonatomic, strong)UIButton * leftButton;
@property (nonatomic, strong)UIButton * middleButton;
@property (nonatomic, strong)UIButton * rightButton;
@property (nonatomic, assign)int index;
@property (nonatomic, copy)NSString * user_id;
@property (nonatomic, copy)NSString * ID;

@end

@implementation XiehouView

- (instancetype)initWithFrame:(CGRect)frame{
    
    if ([super initWithFrame:frame]) {
        
        [self setupUI];
    }
    
    return self;
}


- (void)setModelArray:(NSArray<XiehouModel *> *)modelArray{
    
    _modelArray = modelArray;
    
    self.index = 0;
}


- (void)setIndex:(int)index{
    
    _index = index;
    
    XiehouModel * model = self.modelArray[index];
    
    NSArray * arr = model.headimg;
    
    NSURL * url = arr.firstObject;
    [self.backImageView sd_setImageWithURL:url];
    self.nameLabel.text = model.name;
    self.ageLabel.text = [NSString stringWithFormat:@" %@岁 ",model.age];
    self.startLabel.text = [NSString stringWithFormat:@" %@ ",model.constellation];
    self.adressLabel.text = [NSString stringWithFormat:@" %@ ",model.address];
    self.contentLabel.text = [NSString stringWithFormat:@"个性签名：%@",model.signature];
    self.user_id = model.user_id;
    self.ID = model.ID;
    // timeStampString 是服务器返回的13位时间戳
    NSString *timeStampString  = model.ctime;
    
    // iOS 生成的时间戳是10位
    NSTimeInterval interval    =[timeStampString doubleValue];
    NSDate *date               = [NSDate dateWithTimeIntervalSince1970:interval];
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSString *dateString       = [formatter stringFromDate: date];
    
    self.timeLabel.text = dateString;
}


- (void)setupUI{
    
    UIImageView * backImageView = [UIImageView new];
    
    backImageView.layer.cornerRadius = 13 * SCREENH6;
    backImageView.layer.masksToBounds = YES;
    
    self.backImageView = backImageView;
    
    [self addSubview:self.backImageView];
    
    
    UIView * bottomView = [UIView new];
    
    bottomView.backgroundColor = BLACK_COLOR;
    bottomView.alpha = 0.7;
    
    self.bottomView = bottomView;
    
    [self.backImageView addSubview:self.bottomView];
    
    UILabel * nameLabel = [UILabel new];
    
    nameLabel.textColor = WHITE_COLOR;
    nameLabel.font = [UIFont systemFontOfSize:16 * SCREENH6];
    nameLabel.text = @"----";
    
    self.nameLabel = nameLabel;
    
    [self.backImageView addSubview:self.nameLabel];
    
    
    UILabel * ageLabel = [UILabel new];
    
    ageLabel.backgroundColor = ColorWithRGB(0xF0C4F1);
    ageLabel.textColor = WHITE_COLOR;
    ageLabel.font = [UIFont boldSystemFontOfSize:13 * SCREENH6];
    
    self.ageLabel = ageLabel;
    
    [self.backImageView addSubview:self.ageLabel];
    
    
    UILabel * startLabel = [UILabel new];
    
    startLabel.backgroundColor = ColorWithRGB(0xA6C73A);
    startLabel.textColor = WHITE_COLOR;
    startLabel.font = [UIFont boldSystemFontOfSize:13 * SCREENH6];
    
    self.startLabel = startLabel;
    
    [self.bottomView addSubview:self.startLabel];
    
    
    UILabel * adressLabel = [UILabel new];
    
    adressLabel.backgroundColor = ColorWithRGB(0xB83928);
    adressLabel.textColor = WHITE_COLOR;
    adressLabel.font = [UIFont boldSystemFontOfSize:13 * SCREENH6];
    
    self.adressLabel = adressLabel;
    
    [self.bottomView addSubview:self.adressLabel];
    
    
    UIView * lineView = [UIView new];
    
    lineView.backgroundColor = ColorWithRGB(0x15C4B3);
    
    self.lineView = lineView;
    
    [self.bottomView addSubview:self.lineView];
    
    
    UILabel * contentLabel = [UILabel new];
    
    contentLabel.textColor = WHITE_COLOR;
    contentLabel.font = [UIFont systemFontOfSize:14 * SCREENH6];
    
    self.contentLabel = contentLabel;
    
    [self.bottomView addSubview:self.contentLabel];
    
    
    UILabel * timeLabel = [UILabel new];
    
    timeLabel.textColor = WHITE_COLOR;
    timeLabel.font = [UIFont systemFontOfSize:10 * SCREENH6];
    
    self.timeLabel = timeLabel;
    
    [self.bottomView addSubview:self.timeLabel];
    
    self.leftButton = [self setupButtonWith:@"safddsf22"];
    [self addSubview:self.leftButton];
    
    self.middleButton = [self setupButtonWith:@"safdsf22"];
    [self addSubview:self.middleButton];
    
    self.rightButton = [self setupButtonWith:@"sfdsfds33"];
    [self addSubview:self.rightButton];
}


- (void)layoutSubviews{
    
    [self.backImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self).offset(12.5 * SCREENH6);
        make.trailing.mas_equalTo(self).offset(-12.5 * SCREENH6);
        make.top.mas_equalTo(self).offset(5 * SCREENH6);
        make.height.mas_equalTo(425 * SCREENH6);
    }];
    
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.leading.trailing.mas_equalTo(self.backImageView);
        make.height.mas_equalTo(105 * SCREENH6);
    }];
    
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.bottomView).offset(15 * SCREENH6);
        make.top.mas_equalTo(self.bottomView).offset(12 * SCREENH6);
    }];
    
    [self.ageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.nameLabel);
        make.top.mas_equalTo(self.nameLabel.mas_bottom).offset(10 * SCREENH6);
    }];
    
    [self.startLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.ageLabel);
        make.leading.mas_equalTo(self.ageLabel.mas_trailing).offset(5 * SCREENH6);
    }];
    
    [self.adressLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.startLabel);
        make.leading.mas_equalTo(self.startLabel.mas_trailing).offset(5 * SCREENH6);
    }];
    
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self).offset(5 * SCREENH6);
        make.trailing.mas_equalTo(self).offset(-5 * SCREENH6);
        make.height.mas_equalTo(1 * SCREENH6);
        make.top.mas_equalTo(self.ageLabel.mas_bottom).offset(5 * SCREENH6);
    }];
    
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.ageLabel);
        make.trailing.mas_equalTo(self).offset(-15 * SCREENH6);
        make.top.mas_equalTo(self.lineView.mas_bottom).offset(5 * SCREENH6);
    }];
    
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.contentLabel);
        make.top.mas_equalTo(self.contentLabel.mas_bottom).offset(5 * SCREENH6);
    }];
    
    [self.middleButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self);
        make.top.mas_equalTo(self.backImageView.mas_bottom).offset(3 * SCREENH6);
        make.width.height.mas_equalTo(69.5 * SCREENH6);
    }];
    
    [self.leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(55.5 * SCREENH6);
        make.centerY.mas_equalTo(self.middleButton);
        make.trailing.mas_equalTo(self.middleButton.mas_leading).offset(-17 * SCREENH6);
    }];
    
    [self.rightButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.centerY.mas_equalTo(self.leftButton);
        make.leading.mas_equalTo(self.middleButton.mas_trailing).offset(17 * SCREENH6);
    }];
}


- (UIButton *)setupButtonWith:(NSString *)str{
    
    UIButton * button = [UIButton new];
    
    [button setImage:ImageNamed(str) forState:UIControlStateNormal];
    [button addTarget:self action:@selector(ClickButtomButton:) forControlEvents:UIControlEventTouchUpInside];
    
    return button;
}


- (void)ClickButtomButton:(UIButton *)button{
    
    if (button == self.middleButton) {
        
        NSDictionary * dict = @{@"ID":self.ID};
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"pushToPersonInfoVC" object:nil userInfo:dict];
        
    }else if (button == self.leftButton) {
       
        if (self.index < self.modelArray.count - 1) {
            
            self.index = self.index + 1;
        }else{
            
            self.index = 0;
        }
        
        
    }else{
        
        NSString * user_id = DEF_GET_OBJECT(@"user_id");
        
        NSDictionary * dict = @{@"type":@"1",@"user_id":user_id,@"other_user_id":self.user_id};
        
        [[NetworkTool sharedTools] POST:@"/index/Liuheonline/favor" parameters:dict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            
//            NSLog(@"===%@",responseObject);
            
            int status = [responseObject[@"status"] intValue];
            
            if (status == 200) {
                
                if (self.index < self.modelArray.count - 1) {
                    
                    self.index = self.index + 1;
                }else{
                    
                    self.index = 0;
                }
            }
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
        }];
    }
    
}


@end
